/**
create database bt;
use bt;
CREATE TABLE IF NOT EXISTS `bt1` (
  `id` int(11) PRIMARY KEY auto_increment,
  `title` varchar(200) DEFAULT NULL,
  `size` varchar(20) DEFAULT NULL,
  `description` text,
  `source` varchar(50) DEFAULT NULL COMMENT '1:sukebei',
  `update_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `status` char(1) DEFAULT '1' COMMENT '1:有效;2:无效',
  `bt_path` varchar(200),
  `hash` varchar(40),
  `file_num` smallint(6) DEFAULT '1',
  `source_id` varchar(50) DEFAULT NULL,
  `type` char(1) DEFAULT NULL COMMENT 'a:game'
) ENGINE=InnoDB;
**/
/*
npm install request
npm install mysql
*/
var mysql = require('mysql');
//var pool = mysql.createPool('mysql://root:root@localhost/bt?charset=UTF-8');
var pool = mysql.createPool({
    	host : 'localhost',
    	port : 3306,
        user : 'root',
    	password : 'root',
    	database : 'bt'
});

var request = require("request");

function fetch(page){
	request({//order=2降序
	  url : "https://sukebei.nyaa.eu/?page=search&cats=7_27&sort=1&order=2&offset=" + page,
	  headers : {}
	}, function(error, response, body){
	  if(error)
		throw error;
	  if(!error && response.statusCode == 200){
		var content_type = response.toJSON().headers['content-type'];
		if(content_type && content_type.indexOf("text/html") != -1){
		  insertData(response.body);
		}
	  }
	});
}

var mark = {
    title : ['tlistname"><a href="https://sukebei.nyaa.eu/?page=view&#38;tid=', '</a></td><td class="tlistdownload', '">'],
    size : ['tlistsize">', 'B</td><td class='],
	sub : ['tlistmn', 'tlistrow']
};
var page = 1;
var maxpage = 100;
function insertData(html){
	var title$id = interceptFromSource(html, mark.title[0], mark.title[1]);//190033">A Kiss For The Petals - Maidens of Michael &#40;Demo version&#41;
	var tid = title$id.split(mark.title[2])[0];
	var title = title$id.split(mark.title[2])[1];
	var size = interceptFromSource(html, mark.size[0], mark.size[1]) + "B";
	var data = {tid : tid, title : title.replace('\\', '-'), size : size};
	print(data)
	console.log("page" + page);
	insert(data, function(){
		var temp = subFromSource(html, mark.sub[0]);
		if(temp.indexOf(mark.sub[1]) != -1){
			insertData(temp);
		}else{
			page --;
			if(page >= 1){
				fetch(page);
			}else{
				page = 1;
			}
		}
	});
}

function print(data){
	for(var k in data){
		console.log(k + ":" + data[k]);
	}
}

function interceptFromSource(source, prefix, suffix){
	var s = source;
	s = s.substr(s.indexOf(prefix) + prefix.length, s.length);
    return s.substring(0, s.indexOf(suffix));
}

function subFromSource(source, prefix){
	return source.substr(source.indexOf(prefix) + prefix.length, source.length);
}

function trim(s){
	if(s != null && typeof s == 'string'){
		return s.replace(/(^\s*)|(\s*$)/g, "");
	}
	return s;
};


function insert(data, callback){
	
	pool.query("select * from bt1 where source='1' and source_id = '" + data.tid + "'", function(err, rows, field){
		if(err){
			console.log(err.message);	
			pool.end();
			return;
		}else if(rows.length > 0){
			console.log('数据重复');	
			callback();
			return;
		}
		var sql = "insert into bt1(title, size, source, source_id, type) values(" + mysql.escape(data.title) + "," + mysql.escape(data.size) + ", '1', " + mysql.escape(data.tid) + ", '1')";
		//console.log(sql);
		pool.query(sql, function(err, rows, field){
			if(err){
				console.log(err.message);
				pool.end();
				return;
			}
			callback();
		});
	});
}

fetch(page);
//10分钟一次
setInterval(function(){
	fetch(page);
},1000 * 60 * 10);
